import { observable, action, computed } from 'mobx'
class AppleStore {
    // 通过装饰器监听数据
    @observable appleList = []
    @observable num = 0
    @observable weight = 0
    @observable eat = 0
    @observable eatWeight = 0

    @action.bound addAppleList () {
        const time = new Date()
        this.appleList.push({
            id: time.getTime(),
            name: '红苹果',
            weight: 200
        })
        this.num = this.appleList.length
        if (this.appleList.length > 0){
            this.getTotalWeight()
        }else{
            this.weight = 0
        }
    }
    @action.bound eatAppleList (index) {
        this.num -= 1
        this.eat += 1
        this.eatWeight += this.appleList[index].weight
        this.appleList.splice(index, 1);
        this.getTotalWeight()
    }
    @computed get getAppleList () {
        return this.appleList
    }
    getTotalWeight(){
        let total = 0
        this.appleList.map(item => {
            total += item.weight
        })
        this.weight = total
    }
}

const appleStore = new AppleStore()

export default appleStore